package Server;


import java.io.IOException;
import java.net.*;

public class UDP_Server {

    private static final int PORT = 4664;
    private static DatagramSocket datagramSocket;
    private static DatagramPacket inPacket, outPacket;
    private static byte[] buffer;

    public static void main(String[] args) {
        System.out.println("Opening port...");
        try {

            datagramSocket = new DatagramSocket(PORT); //Step 1.
        } catch (SocketException sockEx) {
            System.out.println(
                    "Unable to attach to port!");
            System.exit(1);
        }

        System.out.println("Connected to port " + PORT + "\n");

        try {
            InetAddress myIPaddress = InetAddress.getLocalHost();
            System.out.println("local IP address : " + myIPaddress.toString());
        } catch (UnknownHostException lhe) {
            System.out.println("Cannot find host");
        }


        handleClient();
    }

    private static void handleClient() {
        try {
            String messageIn, messageOut;
            int numMessages = 0;
            do {
                buffer = new byte[256]; //Step 2.
                inPacket = new DatagramPacket(buffer, buffer.length); //Step 3.
                datagramSocket.receive(inPacket);//Step 4.
                InetAddress clientAddress = inPacket.getAddress(); //Step 5.
                int clientPort = inPacket.getPort(); //Step 5.
                messageIn = new String(inPacket.getData(),
                        0, inPacket.getLength()); //Step 6.
                System.out.println("Message received: " + messageIn);
                System.out.println("Client : " + clientAddress.toString());
                
                switch(messageIn) {
                    case "WHITELIST" : {
                        messageOut = "Whitelist pedida";
                    } break;
                    case "BLACKLIST" : {
                        messageOut = "Blacklist pedida";
                    } break;
                    case "PLAYERS" : {
                        messageOut = "Lista de participantes pedida";
                    } break;
                    case "STARTTIME" : {
                        messageOut = "Hora de inicio do questionario pedida";
                    } break;
                    case "ANSWER" : {
                        messageOut = "Resposta a uma pergunta";
                    } break;
                    case "LEAVE" : {
                        messageOut = "Pedido para sair";
                    }break;
                    default : {
                        messageOut = "ERRO";
                    }
                }  
                
                numMessages++;
                //messageOut = "Message " + numMessages + ": " + messageIn;
                outPacket = new DatagramPacket(messageOut.getBytes(),
                        messageOut.length(), clientAddress,
                        clientPort); //Step 7.
                datagramSocket.send(outPacket); //Step 8.
            } while (true);
        } catch (IOException ioEx) {
            ioEx.printStackTrace();
        } finally //If exception thrown, close connection.
        {
            System.out.println(
                    "\n* Closing connection... *");
            datagramSocket.close(); //Step 9.
        }
    }
}
